package com.lds.erp.category;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.dph.common.utils.base.BaseController;
import com.dph.common.utils.utils.AppUtils;
import com.dph.common.utils.utils.CommonUtils;
import com.lds.erp.base.JsonResult;
import com.lds.erp.category.ao.CategoryConditionQueryAO;
import com.lds.erp.category.vo.CategoryConditionQueryVO;
import com.lds.erp.category.vo.CategoryInfoVo;
import com.lds.erp.category.vo.CategoryVo;
import com.lds.erp.common.YNEnum;
import com.lds.erp.product.categories.ProductCategoriesFacde;
import com.lds.erp.product.categories.order.CategoryConditionLevelOrder;
import com.lds.erp.product.categories.order.CategoryQueryOrder;
import com.lds.erp.product.categories.result.CategoryConditionLevelResult;
import com.lds.erp.product.categories.result.CategoryQueryResult;
import com.lds.erp.product.categories.vo.PtCategoryVo;
import com.lds.erp.util.JsonUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author pengxi
 * @apiNote
 * @date 2018/10/15 下午4:22
 */
@RestController
@RequestMapping(value = "/api/product/category/")
@Api(value = "通用名", description = "商品分类", tags = { "商品分类接口" })

public class CategoryController extends BaseController {
	private final static Logger log = LoggerFactory.getLogger(CategoryController.class);
	@Reference(version = "1.0")
	private ProductCategoriesFacde productCategoriesFacde;

	@GetMapping("list")
	@ApiOperation(value = "获取分类列表(senilon)", notes = "获取分类列表，已排除没有商品的分类")
	public JsonResult list(@RequestParam(value = "catStatus", required = false) String catStatus) {
		CategoryInfoVo categoryInfoVo = new CategoryInfoVo();
		CategoryQueryOrder order = new CategoryQueryOrder();
		if (null != catStatus) {
			order.setCatStatus(YNEnum.getEnumByCode(catStatus));
		}
		//排除没有商品的分类
		order.setDisplayRootCategory(YNEnum.N);
		order.setExcludeWithoutProductCagetory(YNEnum.Y);
		order.setSerialNo(AppUtils.newNo());
		CategoryQueryResult categoryQueryResult = productCategoriesFacde.categoriesQuery(order);
		List<PtCategoryVo> ptCategoryVoList = categoryQueryResult.getPtCategoryList();
		// 树形数据组装
		List<CategoryVo> categoryVoList = new ArrayList<>();
		CategoryVo categoryVo = null;
		for (PtCategoryVo item : ptCategoryVoList) {
			categoryVo = new CategoryVo();
			BeanUtils.copyProperties(item, categoryVo);
			categoryVoList.add(categoryVo);
		}
		if (CommonUtils.isNotEmpty(categoryVoList)) {
			categoryVoList = CategoryVo.toTreeList(categoryVoList);
		}
		categoryInfoVo.setCategoryVoList(categoryVoList);
		return categoryQueryResult.isSuccess() ? JsonUtils.success(categoryInfoVo)
				: JsonUtils.fail(categoryQueryResult.getMessage());
	}

	@GetMapping("query_condition_level_category")
	@ApiOperation(value = "查询对应等级分类(senilon)", notes = "查询对应等级分类")
	public JsonResult queryConditionLevelCategory(CategoryConditionQueryAO ao) {
		CategoryConditionLevelOrder order = new CategoryConditionLevelOrder();
		order.setCatLevel(ao.getCatLevel());
		order.setSerialNo(AppUtils.newNo());
		CategoryConditionLevelResult result = productCategoriesFacde.categoryConditionLevelQuery(order);
		CategoryConditionQueryVO vo = new CategoryConditionQueryVO();
		vo.setCategoryVoList(
				JSON.parseObject(JSON.toJSONString(result.getPtCategoryVos()), new TypeReference<List<CategoryVo>>() {
				}));
		return result.isSuccess() ? JsonUtils.success(vo) : JsonUtils.fail(result.getMessage());
	}
}
